function [  ] = plotHisto5( data, dataUnfiltered, range, name, xName, xMode, maxComp )
if isempty(range)
    range = [min(dataUnfiltered) max(dataUnfiltered)];
end
if strcmp(xMode, 'log10')
    data = log10(data);
    dataUnfiltered = log10(dataUnfiltered);
    range = log10(range);
elseif strcmp(xMode, 'log')
    data = log(data);
    dataUnfiltered = log(dataUnfiltered);
    range = log(range);
end
%% filtering
filtSigma = 3;
filtPercent = 10;

dataFilt = data;
dataOut = [];
for k = 1:12
    sigma = nanstd(dataFilt);
    mu = nanmedian(dataFilt);
    mask = (mu-filtSigma*sigma <= dataFilt) & (dataFilt <= mu+filtSigma*sigma);
%     disp(length(dataFilt) - sum(mask));
    dataOut = [dataOut, dataFilt( ~mask )];
    dataFilt = dataFilt( mask );
end

if isinf(range(1))
    range(1) = min(data);
end

[histCounts, histEdges] = histcounts(dataFilt,ceil(sqrt(length(dataFilt))));
mask = histCounts >= (max(histCounts) * filtPercent/100);
maskIdcs = find(mask > 0);
mask = (histEdges(maskIdcs(1)) <= dataFilt) & (dataFilt <= histEdges(maskIdcs(end)+1));
dataFilt = dataFilt( mask );

%% find n histo peaks
if isempty(maxComp)
    maxComp = min(10,floor(length(dataFilt)/5));
    if maxComp < 1
        maxComp = 1;
    end
end
nVec = 1:maxComp;
% aics = zeros(size(nVec));
bics = zeros(size(nVec));
% nlogLs = zeros(size(nVec));
errorFlag = false;
for n = nVec
    try
        GMModel = fitgmdist(dataFilt',n,'Replicates',10,'Options',statset('MaxIter',1500,'TolFun',1e-5));
        bics(n) = GMModel.BIC;
    catch
        errorFlag = true;
        break;
    end
end

if ~errorFlag
    noOfComp = min(find(bics == min(bics)),maxComp);

    GMModel = fitgmdist(dataFilt',noOfComp,'Replicates',10,'Options',statset('MaxIter',1500,'TolFun',1e-5));
    tmp(:,1) = GMModel.ComponentProportion';
    tmp(:,2) = GMModel.mu;
    tmp(:,3) = squeeze(GMModel.Sigma);
    tmp = sortrows(tmp,1,'descend');
    Comp.ComponentProportion = tmp(:,1);
    Comp.mu = tmp(:,2);
    Comp.Sigma = tmp(:,3);
end

%% plotting
subplot(1,5,1:4);
[histCounts,histEdges] = histcounts(dataFilt,ceil(sqrt(length(dataFilt))));
hh = histogram('BinEdges',histEdges,'BinCounts',histCounts);
set(hh(1),'FaceColor',[61 38 168]/255,'FaceAlpha',1);
x = linspace(range(1),range(2),1e3);
histInt = sum(diff(histEdges) .* histCounts);
hold on;
if ~errorFlag
    % correction to area under the Gaussian pdfs in the interval of the histogram
    histInt = histInt * noOfComp / sum(diff((1/2)*(1+erf((repmat([histEdges(1), histEdges(end)],noOfComp,1)-repmat(Comp.mu,1,2))./repmat(sqrt(Comp.Sigma)*sqrt(2),1,2))),1,2));
    y = Comp.ComponentProportion.*pdf('Normal',x,Comp.mu,sqrt(Comp.Sigma));
    plot(x,histInt*(sum(y,1)), x,histInt*y, 'LineWidth',1.5);
end
hold off;
dBin = histEdges(2) - histEdges(1);
histBinEdges = [fliplr(histEdges(1):-dBin:range(1)), histEdges(1:end-1), histEdges(end):dBin:range(2)];
if length(data) >= 2
    hold on;
    histogram(data,histBinEdges,'FaceColor',[17 119 51]/255,'FaceAlpha',1);
    histogram(dataUnfiltered,histBinEdges,'FaceColor',[232 152 117]/255,'FaceAlpha',1);
    hold off;
    hc = get(gca,'Children');
    if ~errorFlag
        set(gca,'Children',[hc(3:3+noOfComp)', hc(end), hc(2), hc(1)]);
        legend([hc(1), hc(2), hc(end), hc(end-1), hc(3:2+noOfComp)'],[{'all', 'filtered', 'filt for dist fit', 'all comp'} cellstr(num2str((1:noOfComp)', 'comp %-d'))'], 'Location','Best');
    else
        set(gca,'Children',[hc(end), hc(2), hc(1)]);
        legend([hc(1), hc(2), hc(end)],{'all', 'filtered', 'filt for dist fit'}, 'Location','Best');
    end
end
% scale and print results inside
xlabel(xName); ylabel('#');title([name sprintf(' (%d - %d - %d cells)', length(dataUnfiltered), length(data), length(dataFilt))],'Interpreter','none');
med = median(dataFilt);
xlim(range);
yLim = get(gca,'ylim');
set(gca,'TickDir','out');

subplot(1,5,5);
ax = gca; ax.Visible = 'off';
% textXPos = 0.75*(range(2)-range(1)) + range(1);
% text(textXPos,sum([0.55 0.45].*yLim), [sprintf('med = %.5f\n', med) sprintf('comp %d:\nratio = %.2f%%\n\\mu = %.5f\n\\sigma = %.5f\n',[(1:noOfComp)', Comp.ComponentProportion*100,Comp.mu,sqrt(Comp.Sigma)]')] );%, 'VerticalAlignment', 'center');
if ~errorFlag
    text(ax.XLim(1),ax.YLim(2), [sprintf('med = %.5g\n', med) sprintf('comp %d:\nratio = %.2f%%\n\\mu = %.5f\n\\sigma = %.5f\n',[(1:noOfComp)', Comp.ComponentProportion*100,Comp.mu,sqrt(Comp.Sigma)]')] , 'HorizontalAlignment','left','VerticalAlignment','top');
else
    text(ax.XLim(1),ax.YLim(2), sprintf('med = %.5g\n', med) , 'HorizontalAlignment','left','VerticalAlignment','top');
end
end

